package com.chenfwind.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.chenfwind.common.Constant;
import com.chenfwind.common.ResultVO;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Comment;
import com.chenfwind.domain.Friend;
import com.chenfwind.domain.ShareRecode;
import com.chenfwind.domain.User;
import com.chenfwind.domain.vo.CommentVO;
import com.chenfwind.domain.vo.ShareRecodeVO;
import com.chenfwind.service.BookService;
import com.chenfwind.service.FriendService;
import com.chenfwind.service.ShareRecodeService;
import com.chenfwind.service.UserService;

@Controller
@RequestMapping("/share")
public class ShareController {
	
	@Autowired
	private FriendService friendService;
	
	@Autowired
	private ShareRecodeService shareRecodeService;
	
	@Autowired
	private BookService bookService;
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping(value = "/getShareRecodeByUser" , method = RequestMethod.POST)
	public ResultVO<List<ShareRecodeVO>> getMessageByUser(HttpSession session,@RequestParam(value = "userId" ,required = false) String userId){
		ResultVO<List<ShareRecodeVO>> vo = ResultVO.create();
		if (userId == null ) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<ShareRecode> shareRecodeList = shareRecodeService.getRecodeByUser(userId);
		List<ShareRecodeVO> shareVO = new ArrayList<ShareRecodeVO>();
		for (ShareRecode share : shareRecodeList) {
			shareVO.add(ShareRecode2ShareRecodeVO(share,null));
		}
		vo.setResult(shareVO);
		vo.reCalcStatus();
		return vo;
	}
	
	public ShareRecodeVO ShareRecode2ShareRecodeVO(ShareRecode shareRecode ,ShareRecodeVO shareRecodeVO){
		if (shareRecodeVO == null) {
			shareRecodeVO = new ShareRecodeVO();
			shareRecodeVO.setBookName(bookService.getBookNameById(shareRecode.getBookId()));
			shareRecodeVO.setUserName(userService.getAccountByUID(shareRecode.getUserId()));
		}
		com.chenfwind.utils.BeanUtils.copyProperties(shareRecode, shareRecodeVO);
		return shareRecodeVO;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/shareBook", method = RequestMethod.POST)
	public ResultVO<String> shareBook(HttpSession session,
			@RequestParam(value = "shareInfoJson", required = false) String shareInfoJson){
			ResultVO<String> vo = ResultVO.create();
			ShareRecodeVO shareRecodeInfo = JSON.parseObject(shareInfoJson, ShareRecodeVO.class);
			if (shareRecodeInfo == null) {
				return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
			}
			ShareRecode shareRecode = CommentVO2Comment(shareRecodeInfo , null);

			shareRecode.setShareTime(new Date());
			shareRecodeService.save(shareRecode);
			vo.setStatus(Constant.CODE_SUCCESS);
			return vo;
	}
	
	public ShareRecode CommentVO2Comment(ShareRecodeVO shareRecodeVO , ShareRecode shareRecode ){
		if (shareRecode == null) {
			shareRecode = new ShareRecode();
			shareRecode.setId(StringUtils.genUUID());
			shareRecode.setShareTime(new Date());
		}
		com.chenfwind.utils.BeanUtils.copyProperties(shareRecodeVO, shareRecode);
		return shareRecode;
	}
}
